/**
 *====================================================
 * 文件名称: BulkstockCollectionServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.service;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockCollectionEntity;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: BulkstockCollectionServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:53:27
 */
@Implements(BulkstockCollectionService.class)
public class BulkstockCollectionServiceImpl implements BulkstockCollectionService {

	@Override
	@Transactional
	public Long createBulkstockCollection(BulkstockCollectionEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getCollectionId();
	}

	@Override
	public void updateBulkstockCollection(BulkstockCollectionEntity entity) {
		BulkstockCollectionEntity persist = inquireBulkstockCollectionDetail(entity.getCollectionId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	@Transactional
	public void disableBulkstockCollection(Long collectionId) {
		BulkstockCollectionEntity entity = PersistUtil.get(BulkstockCollectionEntity.class, collectionId);
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}

	@Override
	public BulkstockCollectionEntity inquireBulkstockCollectionDetail(Long collectionId) {
		BulkstockCollectionEntity persist = PersistUtil.get(BulkstockCollectionEntity.class, collectionId);
		if (persist == null) {
			throw new BusinessException("大宗商品采集信息不存在");
		}
		return persist;
	}

	@Override
	public PageList<BulkstockCollectionEntity> inquireBulkstockCollectionPageList(Long categoryId, Integer startDate, Integer endDate, String bulkstockName, String bulkstockType, String specification, String manufacturer, String region,
			PageCriteria page) {
		DetachedCriteria dc = DetachedCriteria.forClass(BulkstockCollectionEntity.class).add(Restrictions.eq("enabled", Boolean.TRUE));
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (categoryId != null) {
			dc.add(Restrictions.eq("categoryId", categoryId));
		}
		if (startDate != null) {
			dc.add(Restrictions.ge("collectionDate", startDate));
		}
		if (endDate != null) {
			dc.add(Restrictions.le("collectionDate", endDate));
		}
		if (StringUtil.isNotBlank(bulkstockName)) {
			dc.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
		}
		if (StringUtil.isNotBlank(bulkstockType)) {
			dc.add(Restrictions.like("categoryName", "%" + bulkstockType + "%"));
		}
		if (StringUtil.isNotBlank(specification)) {
			dc.add(Restrictions.like("specification", "%" + specification + "%"));
		}
		if (StringUtil.isNotBlank(manufacturer)) {
			dc.add(Restrictions.like("manufacturer", "%" + manufacturer + "%"));
		}
		if (StringUtil.isNotBlank(region)) {
			dc.add(Restrictions.like("region", "%" + region + "%"));
		}
		dc.addOrder(Order.desc("collectionDate"));
		return PersistUtil.get(dc, page);
	}
}
